package net.sourceforge.simcpux.utils;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.util.Log;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXImageObject;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXTextObject;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import net.sourceforge.simcpux.MyApplication;


/**
 * @author rain  01.16 2018
 */

public class ShareReceive extends BroadcastReceiver {

    private static final String TAG = "ShareReceive";
    private static final int THUMB_SIZE = 150;
//    发送到聊天界面——WXSceneSession
//    发送到朋友圈——WXSceneTimeline
//    添加到微信收藏——WXSceneFavorite
//    private int mTargetScene = SendMessageToWX.Req.WXSceneSession;


    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if ("tv.wx.share".equals(action)) {
            String share_type = intent.getStringExtra("share_type");
            if ("timeline".equals(share_type)) {

                int mTargetScene = SendMessageToWX.Req.WXSceneTimeline;

                //分享朋友圈
                String photo = intent.getStringExtra("photo");
                String input = intent.getStringExtra("input");

                Log.d(TAG, "onReceive: photo = " + photo);
                Log.d(TAG, "onReceive: input = " + input);

                WXMediaMessage msg = new WXMediaMessage();
                SendMessageToWX.Req req = new SendMessageToWX.Req();

                if (TextUtils.isEmpty(photo)) {
                    if (TextUtils.isEmpty(input)) {
                        Log.e(TAG, "onReceive: no message for share　！");
                        return;
                    } else {
                        //发文字

                        WXTextObject textObj = new WXTextObject();
                        textObj.text = input;

                        msg.mediaObject = textObj;
                        msg.description = input;

                        req.transaction = buildTransaction("text");
                        req.message = msg;
                        req.scene = mTargetScene;

                    }
                } else {
                    if (TextUtils.isEmpty(input)) {
                        //发图片
                        WXImageObject imgObj = new WXImageObject();
                        imgObj.setImagePath(photo);

                        msg.mediaObject = imgObj;

                        Bitmap bmp = BitmapFactory.decodeFile(photo);
                        Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, THUMB_SIZE, THUMB_SIZE, true);
                        bmp.recycle();
                        msg.thumbData = Util.bmpToByteArray(thumbBmp, true);

                        req.transaction = buildTransaction("img");
                        req.message = msg;
                        req.scene = mTargetScene;
//                        api.sendReq(req);
                    } else {
                        //发图文
                        //微信sdk不支持图文同时发,临时用野路子方法
                        WxShareBackDoor.sharePhotoToWX(MyApplication.getContext(), input, photo);
                        return;
                    }
                }

                //发送分享请求
                WXAPIFactory.createWXAPI(MyApplication.getContext(), Constants.APP_ID).sendReq(req);

            } else if ("friends".equals(share_type)) {
                //分享给朋友
                int mTargetScene = SendMessageToWX.Req.WXSceneSession;


            }
        }
    }

    private String buildTransaction(final String type) {
        return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();
    }
}
